package com.lyb.spzx.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.lyb.spzx.common.exception.DefinedException;
import com.lyb.spzx.manager.listener.ExcelListener;
import com.lyb.spzx.manager.mapper.CategoryMapper;
import com.lyb.spzx.manager.service.CategoryService;
import com.lyb.spzx.model.entity.product.Category;
import com.lyb.spzx.model.vo.common.ResultCodeEnum;
import com.lyb.spzx.model.vo.product.CategoryExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> findCategoryById(Long parentId) {
        //根据id查询下面所有子分类数据
        List<Category> categoryList = categoryMapper.selectById(parentId);
        if (!CollectionUtils.isEmpty(categoryList)){
            //遍历分类的集合，获取每一个分类数据
            for (Category category : categoryList) {
                int count = categoryMapper.findCountById(category.getId());
                if (count > 0){
                    category.setHasChildren(true);
                }else {
                    category.setHasChildren(false);
                }
             }
        }
        return categoryList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            //设置响应结果类型
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("分类数据","utf-8");
            response.setHeader("Content-disposition","attachment;filename=" + fileName + ".xlsx");

            //查询数据库中的数据
            List<Category> categories = categoryMapper.selectAll();
            List<CategoryExcelVo> categoryExcelVos = new ArrayList<>(categories.size());

            for (Category category : categories) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category,categoryExcelVo,CategoryExcelVo.class);
                categoryExcelVos.add(categoryExcelVo);
            }

            //写出数据到浏览器端
            EasyExcel.write(response.getOutputStream(),CategoryExcelVo.class)
                    .sheet("分类数据").doWrite(categoryExcelVos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            ExcelListener<CategoryExcelVo> excelVoExcel = new ExcelListener<>(categoryMapper);
            //调用read读取数据
            EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,excelVoExcel).sheet().doRead();

        }catch (Exception e){
            throw new DefinedException(ResultCodeEnum.DATA_ERROR);
        }
    }
}
